package com.lambda.swipeback;

import android.annotation.TargetApi;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;

public class UIUtils {
    @TargetApi(21)
    public static ViewGroup a(Window window, View view) {
        if (window == null) {
            return null;
        }

        ViewGroup viewGroup = d(window);
        if (viewGroup != null) {
            return viewGroup;
        }

        if (view != null) {
            ViewParent parent = view.getParent();
            while (parent != window.getDecorView() && parent != null) {
                viewGroup = (ViewGroup) parent;
                parent = parent.getParent();
            }
        }
        if (viewGroup == null) {
            View childAt = ((ViewGroup) window.getDecorView()).getChildAt(0);
            if (childAt instanceof ViewGroup) {
                viewGroup = (ViewGroup) childAt;
                return viewGroup;
            } else if (Build.VERSION.SDK_INT >= 21) {
                window.clearFlags(Integer.MIN_VALUE);
            }
        }
        return viewGroup;
    }

    private static ViewGroup d(Window window) {
        if (Build.VERSION.SDK_INT >= 21) {
            LoaderC<ViewGroup> cVar = new LoaderC<>(window, "mContentRoot", null);
            if (cVar.Xs()) {
                try {
                    return cVar.get();
                } catch (NoSuchFieldException e2) {
                    return null;
                }
            }
        }
        return null;
    }
}
